package org.icrisat.ismab.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*
 *  Created by Swapna nair on 30th Dec 2008
 *  This class defines class DBOperations which extends DBConnection class.
 *	It contains all the database operations such as
 *
 *  1. Insert       ->	DB_Insert()
 *  2. Update       ->	DB_Update()
 *  3. ExecuteQuery ->  DB_ExecuteQuery()
 *  4. Execute      ->  DB_Execute()
 */


public class DBOperations extends DBConnection {
	
	public Boolean DB_Insert(String table, String values){
		
		return this.DB_Execute("insert into "+table+" values ("+values+")");
		
	}
	
	public Boolean DB_Update(String table, String values, String keys){
		
		return this.DB_Execute("Update "+table+" set "+values+" where "+keys);
		
	}
	
	public ResultSet DB_ExecuteQuery(String query){
		 
		 DBConnection db = new DBConnection();
		 Connection con = db.connection();
		 try {
			 
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(query);
			
			/*
			 * The Javadoc for Resultset.isBeforeFirst says that it returns false if the 
			 * cursor is at any other position than before the first row or the result set contains no rows)
			 */
			if(rs.next())
			if(rs.isBeforeFirst()){
				
				return rs;
			}
			con.close();
			
		 } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		 }
		return null; 
	 }
	 
	 public Boolean DB_Execute(String query){
		 
		 DBConnection db = new DBConnection();
		 Connection con = db.connection();
		 try {
			 
			Statement stmt = con.createStatement();			
			stmt.execute(query);
			con.close();
			
		 } catch (SQLException e) {				
				e.printStackTrace();
		 }
		return null; 
	 }
}